import 'package:flutter/material.dart';
import 'package:hzy_project_template/commpontents/commpontents_index.dart';
import 'package:hzy_project_template/config/config_index.dart';
import 'package:hzy_project_template/utils/utils_index.dart';

import '../utils/picker_tool.dart';

class NormalImagePopWidget extends StatefulWidget {
  const NormalImagePopWidget({
    super.key,
    this.type = 0,
    this.maxAssets = 100,
    this.normalAssetS,
  });

  final int type;
  final List? normalAssetS;
  final int maxAssets;

  @override
  State<StatefulWidget> createState() {
    return NormalImagePopState();
  }
}

class NormalImagePopState extends CommonStateWidget<NormalImagePopWidget> {
  List<String> btnList = [];
  List? selectedAssets;
  bool isShowNoPer = false;
  @override
  void initState() {
    configBtnList();
    selectedAssets = widget.normalAssetS;
    super.initState();
  }

  @override
  Widget createBody({
    required BuildContext context,
    BoxConstraints? constraints,
  }) {
    Widget body = Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        normamlTopLine(),
        Gaps.vGap20,
        configListBtnWidget(),
        Gaps.vGap20,
        configCannerBtnWidget(),
        Gaps.vGap10,
      ],
    );

    body = isShowNoPer ? configPerWidget() : body;
    body = Container(
      padding: EdgeInsets.only(
        left: 20.w,
        right: 20.w,
        top: 15.w,
        bottom: configSafeBottomHeight(),
      ),
      decoration: BoxDecoration(
        color: popToolBgColor,
        borderRadius: BorderRadius.vertical(
          top: Radius.circular(16.r),
        ),
      ),
      child: body,
    );
    return body;
  }

  /// 配置权限说明页
  configPerWidget() {
    Widget body = const NormalNoPerWidget();
    return body;
  }

  /// 创建工具按钮
  configListBtnWidget() {
    Widget body = Column(
      children: [
        ...List.generate(
          btnList.length,
          (index) => HzyBtnWidget(
            height: 50.h,
            text: btnList[index],
            fontColor: popToolWColor,
            textStyle: HzyTextStyle.fontBold(
              size: 16.sp,
              color: popToolWColor,
            ),
            tapCall: () {
              tapToolBtn(
                text: btnList[index],
              );
            },
          ),
        ),
      ],
    );
    return body;
  }

  /// 创建取消按钮
  configCannerBtnWidget() {
    Widget body = configOnlyTextBtnWidget(
      text: LaunchIdConfig.canner.tr,
      bgColor: popToolSecondBgColor,
      textStyle: HzyTextStyle.fontMedium(
        size: 16.sp,
        color: personTabSelectColor,
      ),
      height: 48.w,
      tapCall: () {
        Navigator.of(context).pop();
      },
    );
    return body;
  }

  configBtnList() {
    btnList = [];
    switch (widget.type) {
      case 0:
        btnList = [
          "拍一张",
          "从相册选择",
          "查看大图",
        ];
        break;
      case 1:
        btnList = [
          "拍一张",
          "从相册选择",
        ];
        break;
      default:
    }
  }

  /// 点击功能按钮
  tapToolBtn({
    required String text,
  }) async {
    if (text == "拍一张" || text == "从相册选择") {
      var res = await PickerTool.configAtlasPermission(isShowPop: false);
      if (res.isNotEmpty) {
        configSetState(() {
          isShowNoPer = true;
        });
      } else {
        PickerTool.configPickToolWithAtlas(
          context: Get.context!,
          selectedAssets: selectedAssets,
          maxAssets: widget.maxAssets,
          type: text == "拍一张" ? 1 : 0,
          callback: (imageList) {
            if (imageList != null) {
              selectedAssets = imageList;
            }
            Navigator.of(context).pop(
              {
                "type": 0,
                "list": imageList,
              },
            );
          },
        );
      }
    } else if (text == "查看大图") {
      Navigator.of(context).pop(
        {
          "type": 1,
        },
      );
    }
  }
}
